<template>
  <div class="update-gender">
    <van-picker
      title="请选择性别"
      show-toolbar
      :columns="columns"
      :default-index="value"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      @change="onPickerChange"
    />
  </div>
</template>

<script>
import {updateUserProfile} from '@/api/user'
export default {
  name: "UpdateGender",
  data() {
      return {
           columns: ['男', '女'],
           localGender:this.value
      }
  },
  props:{
      value:{
          type:Number,
          required:true
      }
  },
  methods: {
    async onConfirm(){
      this.$toast.loading({
        message:'保存中...',
        forbidClick:true,//禁止背景点击
        duration:0//持续展示
      })
      try {
        const localGender = this.localGender
        await updateUserProfile({
          gender:localGender
        })

        // 更新视图
                this.$emit('input',localGender)
        // 关闭弹出
        this.$emit('close')
        // 提示成功
        this.$toast.success('修改成功')
      } catch (error) {
        this.$toast.fail('修改失败')
      }
    },
    onPickerChange(picker,value,index){
        this.localGender=index
    }
  },
};
</script>

<style>
</style>